package com.example.mapster;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import android.support.v4.app.NavUtils;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.os.Build;

public class Request_Friend extends Activity {

	protected static final View EditText = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		final Database db = new Database(this);
		setContentView(R.layout.activity_request__friend);
		// Show the Up button in the action bar.
		setupActionBar();
		findViewById(R.id.cancel).setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View view) {
						// TODO Auto-generated method stub
						Intent intent = new Intent(Request_Friend.this, Friends.class);
						startActivity(intent);
					}
					
				});
		findViewById(R.id.submit).setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View view) {
						// send request
						EditText idField;
						idField = (EditText) findViewById(R.id.idNumber);
						String username = idField.getText().toString();
						CloudHelper ch = new CloudHelper();
						GetRequestedUserId task = new GetRequestedUserId();
						task.execute(username);
						int id = -1;
						try {
							id = task.get();
						}
						catch (Exception e) {
							id = -1;
						}
						if(id > 0){
							//send request here
							db.open();
							User curUser = db.CheckUserLoggedIn();
							new SendFriendRequest().execute(id, curUser.CloudId);
							//return to friends screen
							Context context = getApplicationContext();
							Toast toast = Toast.makeText(context,"Success, your request is sent.", Toast.LENGTH_LONG);
							toast.show();
							Intent intent = new Intent(Request_Friend.this,Friends.class);
							startActivityForResult(intent, 0);
						}else{
							Context context = getApplicationContext();
							Toast toast = Toast.makeText(context,"This user does not exist.", Toast.LENGTH_LONG);
							toast.show();
						}
					}
				});
	}

	/**
	 * Set up the {@link android.app.ActionBar}, if the API is available.
	 */
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	private void setupActionBar() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			getActionBar().setDisplayHomeAsUpEnabled(true);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.request__friend, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	public class GetUserCloudId extends AsyncTask<String, Void, Integer> {
		@Override
		protected Integer doInBackground(String... params) {
			return new CloudHelper().GetCloudIdForUser(params[0]);
		}
	}
	
	public class GetRequestedUserId extends AsyncTask<String, Void, Integer> {
		@Override
		protected Integer doInBackground(String... params) {
			return new CloudHelper().GetCloudIdForUser(params[0]);
		}
	}
	
	public class SendFriendRequest extends AsyncTask<Integer, Void, Void> {
		@Override
		protected Void doInBackground(Integer... params) {
			new CloudHelper().sendFriendRequest(params[0], params[1]);
			return null;
		}
	}
}
